var app=sm("do_App");
var page=sm("do_Page");
var nf = sm("do_Notification");
var config = require("config");
var tools=require("tools");
var cache=sm("do_DataCache");

var do_back=ui("do_back");
var do_ALayout_root=ui("do_ALayout_root");
//用于等待登录的过程
do_ALayout_root.add("loadingUI", "source://view/loadingUI.ui", 0, 0);
var loadingUI = ui("loadingUI");
do_back.on("touch",function(){	
	app.closePage();
});
var companywebview;
httpdata();
function httpdata(datetime){
	loadingUI.visible = true;
	var http = mm("do_Http");
	var nowdate=new Date();
	var olddate=nowdate.getTime();
	app.fire("tologin",{times:olddate,pagesize:3});	
	var crrtime=new Date();
	if(datetime){
		crrtime=new Date(datetime);    	
	}
	http.method = "POST";
	http.timeout = 10 * 1000;
	http.contentType = "application/json";
	http.url = config.baseUrl + "getCompanyStream";
	var userinfo=cache.loadData("userinfo");
	var pagepar=page.getData();  
	http.body=JSON.stringify({companyId:pagepar.companyId,year:crrtime.getFullYear(),month:crrtime.getMonth()+ 1,usernum:userinfo.usernum,userId:userinfo.userid});
	http.on("success", function(data) {	
		if(data.result==1 && data.data && data.data.IncomeList && data.data.PayList){
			if(datetime){				
				page.fire("reloadresult",data.data);				
			}	
			else{
				do_ALayout_root.add("companywebview", "source://view/stream/streamwebview.ui", 0, 89);
				companywebview=ui("companywebview");
				var hashdata = mm("do_HashData");
				companywebview.bindData(hashdata);
				hashdata.addData({
					"chartdata" : data.data
				});
				companywebview.refreshData();
			}
			loadingUI.visible = false;  
			companywebview.visible=true;
			
		}
		else if(data.result==3045){	
			loadingUI.visible = false;
			 app.closePage({data:"帐号可能被其它终端登录，请重新登录", animationType:"", layer:3});	
			 cache.saveData("userinfo", "");
		}
		else{
			loadingUI.visible = false;			 
			nf.toast(data.msg || "失败"); 			
			if(datetime){				
				companywebview.visible=true;	
				page.fire("reloadresult","");
			}	
			else{
				do_ALayout_root.add("companywebview", "source://view/stream/streamwebview.ui", 0, 89);
				companywebview=ui("companywebview");
				var hashdata = mm("do_HashData");
				companywebview.bindData(hashdata);
				hashdata.addData({
					"chartdata" : data.data
				});
				companywebview.refreshData();
			}
			
		}
	});
	http.on("fail", function(data) {
		loadingUI.visible = false;
		nf.toast("网络异常");
	});
	http.request();
}

page.on("reload",function(datetime){
	companywebview.visible=false;
	httpdata(datetime);	
});
page.on("back",function(){
	app.closePage();
});

